﻿/***********************************************************************
 * <copyright file="BusinessObject.cs" company="BUCA JSC">
 * -->    Copyright (C) statement. All right reserved
 * </copyright>
 * 
 * Created:   LinhMC
 * Email:    linhmc@buca.vn
 * Website:
 * Create Date: Thursday, October 17, 2013
 * Usage: 
 * 
 * RevisionHistory: 
 * Date         Author               Description 
 * 
 * ************************************************************************/

using System.Collections.Generic;
using BusinessObjects.BusinessRules;

namespace BusinessObjects
{
    /// <summary>
    ///     Abstract base class for business objects.
    ///     Contains basic business rule infrastructure.
    /// </summary>
    public abstract class BusinessObject
    {
        /// <summary>
        ///     Default value for version number (used in LINQ's optimistic concurrency)
        /// </summary>
        protected static readonly string VersionDefault = "NotSet";

        // List of business rules
        private readonly List<BusinessRule> _businessRules = new List<BusinessRule>();

        // List of validation errors (following validation failure)
        private readonly List<string> _validationErrors = new List<string>();

        /// <summary>
        ///     Gets list of validations errors.
        /// </summary>
        public List<string> ValidationErrors
        {
            get { return _validationErrors; }
        }

        /// <summary>
        ///     Adds a business rule to the business object.
        /// </summary>
        /// <param name="rule"></param>
        protected void AddRule(BusinessRule rule)
        {
            _businessRules.Add(rule);
        }

        /// <summary>
        ///     Determines whether business rules are valid or not.
        ///     Creates a list of validation errors when appropriate.
        /// </summary>
        /// <returns></returns>
        public bool Validate()
        {
            bool isValid = true;

            _validationErrors.Clear();

            foreach (BusinessRule rule in _businessRules)
            {
                if (!rule.Validate(this))
                {
                    isValid = false;
                    _validationErrors.Add(rule.ErrorMessage);
                }
            }
            return isValid;
        }
    }
}